var baseURL = ctx + "/activiti/process";
$(function() {
	load();
});

function load() {
	$("#jqGrid").jqGrid({
		url: baseURL + '/list',
		datatype: "json",
		colModel: [
			{ label: '部署编号', name: 'deploymentId', width: 65,key: true,search: false,hidden: true },
			{ label: '流程KEY', name: 'key', width: 65,search: false },
			{ label: '版本号', name: 'version', index: "version", width: 60,search: false },
			{ label: '流程名称', name: 'name', sortable: false, width: 75 },
			{ label: '流程XML', name: 'id', width: 60,search: false,formatter:function (value, options, row) {
				var e = '<a   href="/activiti/process/resource/read/xml/'+value+'"  title="xml" target="_blank">'+row.resourceName+'</a> ';
				return e;
			}},
			{ label: '流程图片', name: 'id', width: 50,search: false,formatter:function (value,options, row) {
				var e = '<a   href="/activiti/process/resource/read/image/'+value+'"  title="图片" target="_blank">图片</a> ';
				return e;
			}}
		],
		viewrecords: true,
		height: 'auto',
		rowNum: 10,
		rowList : [10,30,50],
		rownumbers: true,
		rownumWidth: 25,
		autowidth:true,
		multiselect: true,
		pager: "#jqGridPager",
		jsonReader : {
			root: "page.list",
			page: "page.currPage",
			total: "page.totalPage",
			records: "page.totalCount"
		},
		prmNames : {
			page:"page",
			rows:"limit",
			order: "order"
		},
		gridComplete:function(){
			//隐藏grid底部滚动条
			$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
		}
	});
	$("#jqGrid").filterToolbar({}).jqGrid('navGrid',"#jqGridPager",{//刷新按钮
		refresh: true,
		edit:false,del: false,search:false,add:false
	});
}

function remove(id) {
	layer.confirm('确定要删除选中的记录？', {
		btn : [ '确定', '取消' ]
	}, function() {
		$.ajax({
			url : prefix+"/remove",
			type : "post",
			data : {
				'id' : id
			},
			success : function(r) {
				if (r.code == 0) {
					layer.msg(r.msg);
					reLoad();
				} else {
					layer.msg(r.msg);
				}
			}
		});
	})
}
function edit(id) {
	var page = layer.open({
		type : 2,
		title : '修改模型',
		maxmin : true,
		shadeClose : false,
		area : [ '100%', '100%' ],
		content : prefix + '/edit/' + id
	});
}

function batchRemove() {
	var rows = $('#exampleTable').bootstrapTable('getSelections'); // 返回所有选择的行，当没有选择的记录时，返回一个空数组
	if (rows.length == 0) {
		layer.msg("请选择要删除的数据");
		return;
	}
	layer.confirm("确认要删除选中的'" + rows.length + "'条数据吗?", {
		btn : [ '确定', '取消' ]
	// 按钮
	}, function() {
		var ids = new Array();
		// 遍历所有选择的行数据，取每条数据对应的ID
		$.each(rows, function(i, row) {
			ids[i] = row['deploymentId'];
		});
		$.ajax({
			type : 'POST',
			data : {
				"ids" : ids
			},
			url : prefix + '/batchRemove',
			success : function(r) {
				if (r.code == 0) {
					layer.msg(r.msg);
					reLoad();
				} else {
					layer.msg(r.msg);
				}
			}
		});
	}, function() {});
}
var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
		},
		showList: true,
		title:null
	},
	methods: {
		query: function () {
			vm.reload();
		},
		covertToModel: function(){
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			confirm('确定要把流程转转换成流程吗吗？',function() {
				$.ajax({
					url : baseURL+"/convertToModel/"+id,
					type : "get",
					success : function(r) {
						if (r.code == 0) {
							alert('操作成功', function(){
								vm.reload();
							});
						} else {
							alert(r.message);
						}
					}
				});
			})
		},
		update: function(){
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			window.open(baseURL + '/edit/' + id);
		},
		deploy: function(){
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			confirm('确定要部署选中的模型吗？',function() {
				$.ajax({
					url : baseURL+"/deploy/"+id,
					type : "post",
					data : {
						'id' : id
					},
					success : function(r) {
						if (r.code == 0) {
							alert('操作成功', function(){
								vm.reload();
							});
						} else {
							alert(r.message);
						}
					}
				});
			})
		},
		del: function () {
			var userIds = getSelectedRows();
			if(userIds == null){
				return ;
			}

			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type : 'POST',
					data : {
						"ids" : userIds
					},
					url : baseURL + '/batchRemove',
					success : function(r) {
						if (r.code == 0) {
							alert('操作成功', function(){
								vm.reload();
							});
						} else {
							alert(r.message);
						}
					}
				});
			});
		},
		reload: function () {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
				postData:{},
				page:page
			}).trigger("reloadGrid");
		}
	}
});